﻿#include "doctorui.h"
#include "ui_doctorui.h"

doctorUI::doctorUI(QString account,QSqlDatabase* db,QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::doctorUI)
{
    ui->setupUi(this);

    this->setCentralWidget(ui->stackedWidget);

    m_db = *db;//初始化数据库连接
    m_account = account;//初始化医生账号

    iniUI();//初始化
    ui->stackedWidget->setCurrentWidget(ui->pagMain);//设置默认界面为main界面

}

doctorUI::~doctorUI()
{
    delete ui;
}

void doctorUI::iniUI()
{
    //初始化界面文本显示
    QSqlQuery query;
    query.exec("select * from tb_doctor where account='"+m_account+"';");
    if(query.size() == 0){//如果没找到该账号
        QMessageBox::warning(0,"Warning","initialization failure0");
        return;
    }

    query.first();
    if(query.isValid()){

        //main界面
        ui->labId->setText(query.value("id").toString());
        ui->labAccount->setText(query.value("account").toString());
        ui->labAge->setText(query.value("age").toString());
        ui->labSex->setText(query.value("sex").toString());
        ui->labPhone->setText(query.value("phone").toString());
        ui->labAddress->setText(query.value("address").toString());

        //修改界面
        ui->labIdChangeDoc->setText(query.value("id").toString());
        ui->editPswdDoc->setText(query.value("pswd").toString());
        ui->labAccountChangeDoc->setText(query.value("account").toString());
        ui->spinAgeChangeDoc->setValue(query.value("age").toString().toInt());
        if(query.value("sex").toString() == "Boy"){
            ui->combSexChangeDoc->setCurrentIndex(0);
        }else{
            ui->combSexChangeDoc->setCurrentIndex(1);
        }
        ui->editPhoneChangeDoc->setText(query.value("phone").toString());
        ui->editAddressChangeDoc->setText(query.value("address").toString());
    }

    m_reserveModel = new QSqlTableModel(this);//预约数据模型
    m_caseModel = new QSqlTableModel(this);//病例数据模型

    //设置表名
    m_reserveModel->setTable("tb_reserve");
    m_caseModel->setTable("tb_case");

    //设置视图自动拉伸和不可编辑
    ui->tvServe->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tvCase->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tvServe->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tvCase->setEditTriggers(QAbstractItemView::NoEditTriggers);

    //同步数据到模型
    m_reserveModel->select();
    m_caseModel->select();

    //绑定模型和视图
    ui->tvServe->setModel(m_reserveModel);
    ui->tvCase->setModel(m_caseModel);



    //修改表头
//    m_reserveModel->setHeaderData(0,Qt::Horizontal,"编号");
//    m_reserveModel->setHeaderData(1,Qt::Horizontal,"病人编号");
//    m_reserveModel->setHeaderData(2,Qt::Horizontal,"病人账号");
//    m_reserveModel->setHeaderData(3,Qt::Horizontal,"预约时间");
//    m_reserveModel->setHeaderData(4,Qt::Horizontal,"类型");


//    m_caseModel->setHeaderData(0,Qt::Horizontal,"编号");
//    m_caseModel->setHeaderData(1,Qt::Horizontal,"病人编号");
//    m_caseModel->setHeaderData(2,Qt::Horizontal,"病人账号");
//    m_caseModel->setHeaderData(3,Qt::Horizontal,"医生编号");
//    m_caseModel->setHeaderData(4,Qt::Horizontal,"医生账号");
//    m_caseModel->setHeaderData(5,Qt::Horizontal,"描述");
//    m_caseModel->setHeaderData(6,Qt::Horizontal,"处方");
}

void doctorUI::on_pushButton_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->pagReserve);
}


void doctorUI::on_pushButton_5_clicked()
{
    this->close();
}


void doctorUI::on_actReserve_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pagReserve);
}


void doctorUI::on_actChange_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pagChange);
}


void doctorUI::on_actCase_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pagCase);
}


void doctorUI::on_actMain_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pagMain);
}


void doctorUI::on_pushButton_2_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->pagChange);
}


void doctorUI::on_pushButton_3_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->pagCase);
}


void doctorUI::on_actQuit_triggered()
{
    this->close();
}


void doctorUI::on_btnResetDoc_clicked()
{
    QSqlQuery query;
    query.exec("select * from tb_doctor where account='"+m_account+"';");
    if(query.size() == 0){//如果没找到该账号
        QMessageBox::warning(0,"Warning","Initialization failure");
        return;
    }

    query.first();
    if(query.isValid()){

        //修改界面
        ui->labIdChangeDoc->setText(query.value("id").toString());
        ui->editPswdDoc->setText(query.value("pswd").toString());
        ui->labAccountChangeDoc->setText(query.value("account").toString());
        ui->spinAgeChangeDoc->setValue(query.value("age").toString().toInt());
        if(query.value("sex").toString() == "Boy"){
            ui->combSexChangeDoc->setCurrentIndex(0);
        }else{
            ui->combSexChangeDoc->setCurrentIndex(1);
        }
        ui->editPhoneChangeDoc->setText(query.value("phone").toString());
        ui->editAddressChangeDoc->setText(query.value("address").toString());
    }
}


void doctorUI::on_btnSubmitChangeDoc_clicked()
{
    if(ui->editPswdDoc->text().isEmpty()){
        QMessageBox::warning(0,"Warning","The password cannot be empty.");
        return;
    }
    QSqlQuery query;
    if(query.exec("update tb_doctor set pswd='"+ui->editPswdDoc->text()+"',age='"+ui->spinAgeChangeDoc->text()+"',sex='"+ui->combSexChangeDoc->currentText()+"',phone='"+ui->editPhoneChangeDoc->text()+"',address='"+ui->editAddressChangeDoc->text()+"' where account='"+m_account+"';")){
        QMessageBox::information(0,"Information","Modify successfully!");

        //修改界面信息
        QSqlQuery query;
        query.exec("select * from tb_doctor where account='"+m_account+"';");
        if(query.size() == 0){//如果没找到该账号
            QMessageBox::warning(0,"Warning","Initialization failure.");
            return;
        }

        query.first();
        if(query.isValid()){

            //main界面
            ui->labId->setText(query.value("id").toString());
            ui->labAccount->setText(query.value("account").toString());
            ui->labAge->setText(query.value("age").toString());
            ui->labSex->setText(query.value("sex").toString());
            ui->labPhone->setText(query.value("phone").toString());
            ui->labAddress->setText(query.value("address").toString());

        }
        return;
    }else{
        QMessageBox::warning(0,"Warning","Modify failure.");
        return;
    }
}


void doctorUI::on_tvServe_doubleClicked(const QModelIndex &index)
{
    ui->labReserveId->setText(m_reserveModel->data(m_reserveModel->index(index.row(),0)).toString());
    ui->labSickId->setText(m_reserveModel->data(m_reserveModel->index(index.row(),1)).toString());
    ui->labSickAccount->setText(m_reserveModel->data(m_reserveModel->index(index.row(),2)).toString());
    ui->labReserveTIme->setText(m_reserveModel->data(m_reserveModel->index(index.row(),3)).toString());
    ui->labType->setText(m_reserveModel->data(m_reserveModel->index(index.row(),4)).toString());

    ui->stackedWidget->setCurrentWidget(ui->pagDetail);
}


void doctorUI::on_btnCancel_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->pagReserve);
}


void doctorUI::on_btnSubmit_clicked()
{
    QSqlQuery query;
    if(query.exec("insert into tb_case(sickid,sickaccount,docid,docaccount,introducce,pre) values('"+ui->labSickId->text()+"','"+ui->labSickAccount->text()+"','"+ui->labId->text()+"','"+ui->labAccount->text()+"','"+ui->editIntroduce->toPlainText()+"','"+ui->editPre->toPlainText()+"');")){
        QMessageBox::information(0,"Information","Submit successfully!");
    }else{
        QMessageBox::warning(0,"Warning","Submit failure!");
        return;
    }

    query.exec("delete from tb_reserve where id='"+ui->labReserveId->text()+"';");

    m_reserveModel->select();
    m_caseModel->select();
}

